package test.marui.com.mytest.ui.viewPagerTest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import test.marui.com.mytest.R;

/**
 * Created by MR on 2019/2/21.
 */
public class ViewPagerTestActivity extends AppCompatActivity {

    private TabLayout tl_head;
    private ViewPager vp_content;
    private List<Fragment> mFragmentList;
    private ViewPagerTestActivity context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_test);

        context = this;

        initView();
        initData();
    }

    private void initView() {
        tl_head = findViewById(R.id.tl_head);
        vp_content = findViewById(R.id.vp_content);
    }

    private void initData() {
        ArrayList<String> data1 = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            data1.add("课程" + i);
        }
        data1.add("结束");

        ArrayList<String> data2 = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            data2.add("成绩" + i);
        }
        data2.add("结束");

        ArrayList<String> mTitleList = new ArrayList<>();
        mTitleList.add("标题1");
        mTitleList.add("标题2");


        mFragmentList = new ArrayList();
        mFragmentList.add(ListFragment.newInstance(data1));
        mFragmentList.add(ListFragment.newInstance(data2));

        vp_content.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),context,mFragmentList,mTitleList));

        tl_head.setupWithViewPager(vp_content);
    }

    private class MyFragmentAdapter extends FragmentPagerAdapter {

        private final List<Fragment> mFragmentList;
        private final ArrayList<String> mTitleList;
        private ViewPagerTestActivity context;

        MyFragmentAdapter(FragmentManager manager, ViewPagerTestActivity context, List<Fragment> fragmentList, ArrayList<String> titleList) {
            super(manager);
            this.context = context;
            mFragmentList = fragmentList;
            mTitleList = titleList;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }
}
